package com.zyx.springredispub.controller;

import com.zyx.springredispub.cons.Channels;
import com.zyx.springredispub.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zyx
 * @since 2022/3/30 23:47
 * desc: 消息发布订阅 Controller 层
 */
@Slf4j
@RestController
@RequestMapping("/pub")
public class RedisPubController {

    @Resource
    private RedisService redisService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/ping")
    public String ping() {
        return "pong";
    }

    @PostMapping(value = "/set")
    public String setAndGet(String msg) {
        stringRedisTemplate.opsForValue().set("keyyyy", msg);
        return stringRedisTemplate.opsForValue().get("keyyyy");
    }

    @PostMapping(value = "/first")
    public String sendMessage(String msg) throws InterruptedException {
        log.info("当前时间：{}", System.currentTimeMillis());
        TimeUnit.SECONDS.sleep(2);
        redisService.sendMessage(Channels.CH01.getChName(), msg);
        return "66666";
    }

    @PostMapping(value = "/second")
    public String sendMessage2(String msg) throws InterruptedException {
        log.info("当前时间：{}", System.currentTimeMillis());
        TimeUnit.SECONDS.sleep(2);
        redisService.sendMessage(Channels.CH02.getChName(), msg);
        return "66666";
    }
}
